package com.example.myapplication.ui.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;

import java.util.List;

public class CategoryCourseAdapter extends RecyclerView.Adapter<CategoryCourseAdapter.ViewHolder> {
    private Context context;
    private List<Course> courses;

    public CategoryCourseAdapter(Context context, List<Course> courses) {
        this.context = context;
        this.courses = courses;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 加载课程item布局（复用之前的item_course.xml）
        View view = LayoutInflater.from(context)
                .inflate(R.layout.item_course, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Course course = courses.get(position);
        // 加载课程图片
        Glide.with(context)
                .load(course.getImageUrl())
                .placeholder(R.drawable.placeholder_image)
                .error(R.drawable.error_image)
                .into(holder.courseImage);
        // 设置课程信息
        holder.courseTitle.setText(course.getTitle());
        holder.courseInstructor.setText("讲师: " + course.getInstructor());
        holder.courseDifficulty.setText("难度: " + course.getDifficulty());
        holder.courseRating.setText("评分: " + course.getRating());
    }

    @Override
    public int getItemCount() {
        return courses.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView courseImage;
        TextView courseTitle;
        TextView courseInstructor;
        TextView courseDifficulty;
        TextView courseRating;

        ViewHolder(View itemView) {
            super(itemView);
            courseImage = itemView.findViewById(R.id.courseImage);
            courseTitle = itemView.findViewById(R.id.courseTitle);
            courseInstructor = itemView.findViewById(R.id.courseInstructor);
            courseDifficulty = itemView.findViewById(R.id.courseDifficulty);
            courseRating = itemView.findViewById(R.id.courseRating);
        }
    }
}
